<?php

/**
 * This is the model class for table "team".
 *
 * The followings are the available columns in table 'team':
 * @property integer $id
 * @property integer $owner_id
 * @property string $name
 * @property string $official_name
 * @property string $state
 * @property string $city
 * @property string $country
 * @property string $street
 * @property integer $number
 * @property string $complement
 * @property string $neighborhood
 * @property string $cep
 * @property string $phone
 * @property string $fax
 * @property string $site
 * @property string $email
 * @property string $cnpjl
 * @property string $insertion_date
 * @property string $federation
 * @property string $mandato
 * @property string $agency
 * @property string $account
 * @property string $bank
 * @property string $status
 *
 * The followings are the available model relations:
 * @property Category[] $categories
 * @property Contact[] $contacts
 * @property Owner $owner
 */
class Team extends CActiveRecord {
	
	public static $photo_name = 'photo_team_';
	
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'team';
	}
	
	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave() {
		parent::afterSave ();
	}
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (
				array (
						'owner_id, name, official_name, state, city, street, number, neighborhood, cep, phone, email, cnpjl, insertion_date, mandato',
						'required' 
				),
				array (
						'owner_id, number, cnpjl',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'name, official_name, street, complement, site, email',
						'length',
						'max' => 250 
				),
				array (
						'name, official_name',
						'unique' 
				),
				array (
						'official_name, email, cnpjl',
						'unique' 
				),
				array (
						'state, city, country, neighborhood, cep, phone, fax, cnpjl, federation, mandato, agency, account, bank, status',
						'length',
						'max' => 45 
				),
				// The following rule is used by search().
				// @todo Please remove those attributes that should not be searched.
				array (
						'id, owner_id, name, official_name, state, city, country, street, number, complement, neighborhood, cep, phone, fax, site, email, cnpjl, insertion_date, federation, mandato, agency, account, bank, status',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
				'categories' => array (
						self::HAS_MANY,
						'Category',
						'team_id' 
				),
				'contacts' => array (
						self::HAS_MANY,
						'Contact',
						'team_id' 
				),
				'owner' => array (
						self::BELONGS_TO,
						'Owner',
						'owner_id' 
				) 
		);
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'id' => 'ID',
				'owner_id' => 'Proprietário',
				'name' => 'Nome',
				'official_name' => 'Nome Oficial',
				'state' => 'Estado',
				'city' => 'Cidade',
				'country' => 'País',
				'street' => 'Rua',
				'number' => 'Número',
				'complement' => 'Complemento',
				'neighborhood' => 'Bairro',
				'cep' => 'Cep',
				'phone' => 'Telefone',
				'fax' => 'Fax',
				'site' => 'Site',
				'email' => 'Email',
				'cnpjl' => 'Cnpj',
				'insertion_date' => 'Data de Registro',
				'federation' => 'Federação',
				'mandato' => 'Mandato',
				'agency' => 'Agencia',
				'account' => 'Conta',
				'bank' => 'Banco',
				'status' => 'Status' 
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 *         based on the search/filter conditions.
	 */
	public function search() {
		// @todo Please modify the following code to remove attributes that should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'owner_id', $this->owner_id );
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'official_name', $this->official_name, true );
		$criteria->compare ( 'state', $this->state, true );
		$criteria->compare ( 'city', $this->city, true );
		$criteria->compare ( 'country', $this->country, true );
		$criteria->compare ( 'street', $this->street, true );
		$criteria->compare ( 'number', $this->number );
		$criteria->compare ( 'complement', $this->complement, true );
		$criteria->compare ( 'neighborhood', $this->neighborhood, true );
		$criteria->compare ( 'cep', $this->cep, true );
		$criteria->compare ( 'phone', $this->phone, true );
		$criteria->compare ( 'fax', $this->fax, true );
		$criteria->compare ( 'site', $this->site, true );
		$criteria->compare ( 'email', $this->email, true );
		$criteria->compare ( 'cnpjl', $this->cnpjl, true );
		$criteria->compare ( 'insertion_date', $this->insertion_date, true );
		$criteria->compare ( 'federation', $this->federation, true );
		$criteria->compare ( 'mandato', $this->mandato, true );
		$criteria->compare ( 'agency', $this->agency, true );
		$criteria->compare ( 'account', $this->account, true );
		$criteria->compare ( 'bank', $this->bank, true );
		$criteria->compare ( 'status', $this->status, true );
		
		if ($_SESSION ['user']->username != 'admin') {
			
			$criteria->condition = 't.status = :status OR t.owner_id = :owner_id';
			$criteria->params = array (
					':status' => 'ATIVO',
					':owner_id' => $_SESSION ['owner']->id );
		} else {
			$criteria->condition = 't.status = :status OR t.status = :status2';
			$criteria->params = array (
					':status' => 'ATIVO',
					':status2' => 'Em Aprovação' );
		}
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * 
	 * @param string $className
	 *        	active record class name.
	 * @return Team the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	public function getDateTime() {
		return Yii::app ()->dateFormatter->format ( Utils::$dateTimeOutcomeFormat, 
				CDateTimeParser::parse ( $this->insertion_date, Utils::$dateTimeIncomeFormat ) );
	}
	public function setDateTime($datetime) {
		$this->insertion_date = Yii::app ()->dateFormatter->format ( Utils::$dateTimeIncomeFormat, 
				CDateTimeParser::parse ( $datetime, Utils::$dateTimeOutcomeFormat ) );
	}
}
